/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *
 */
package org.mizartools.dli;

import java.util.LinkedList;

public class Aggregate extends ItemDefinition {

	private LinkedList<Locus> locusList = new LinkedList<Locus>();
	private Type type = null;
	
	private Aggregate(){
		super(null);
	}

	public Aggregate(LinkedList<Locus> locusList, Type type) throws DliException {
    	super(null);
		if (locusList == null){
			throw new DliException();
		}
		this.locusList = locusList;
		this.type = type;
    }

	public static ItemDefinition newInstance(String string) throws DliParseException {
		Aggregate aggregate = new Aggregate();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in Aggregate");
		}
		if (!string.substring(0, position).equals("aggr")) {
			throw new DliParseException("["+string+"] is not valid in Aggregate");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		String type = null;
		while (positionComma != -1) {
			position = string.indexOf("(");
			if (position == -1) {
				throw new DliParseException("["+string+"] is not valid in Aggregate");
			}
			type = string.substring(0, position);
			if (type.equals("$locus")){
				aggregate.locusList.add(Locus.newInstance(string.substring(0, positionComma)));
			} else if (type.equals("$type")){
				aggregate.type = Type.newInstance(string.substring(0, positionComma));
			} else {
				throw new DliParseException("["+string+"] is not valid in Aggregate");
			}
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in Aggregate");
		}
		type = string.substring(0, position);
		if (type.equals("$locus")){
			aggregate.locusList.add(Locus.newInstance(string));
		} else if (type.equals("$type")){
			aggregate.type = Type.newInstance(string);
		} else {
			throw new DliParseException("["+string+"] is not valid in Aggregate");
		}
		return aggregate;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("aggr(");
		boolean first = true;
		for (Locus locus : locusList){
			if (first) first = false;
			else sb.append(",");
			sb.append(locus.toString());
		}
		if (type != null){
			if (locusList.size()>0) {
				sb.append(",");
			}
			sb.append(type.toString());
		}
		sb.append(")");
		return sb.toString();
		
	}
	
}
